# -*- coding: utf-8 -*-

"""
* Copyright (C) 2016 GridSafe, Inc.
"""
from django.db import models
from django.contrib.auth.models import User

from apps.wallet.models.friend import Follow


class Comment(models.Model):
    """ 评论 """
    id = models.AutoField(primary_key=True)
    type = models.IntegerField(default=1)
    user = models.ForeignKey(User)
    content = models.TextField()
    content_show = models.TextField(null=True)
    reply_id = models.IntegerField(default=0)  # 回复过的评论
    reply_user = models.IntegerField(default=0)  # 回复谁
    add_date = models.DateTimeField(auto_now=True)

    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        pass


class Type(models.Model):
    """分类"""
    name = models.CharField(max_length=100)
    user = models.IntegerField()
    add_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'type'


class Theme(models.Model):
    """编程主题"""
    TYPE = (
        (0, u'未进行'),
        (1, u'进行中'),
        (2, u'已完成'),
        (3, u'任务失败'),
    )
    title = models.CharField(max_length=100)
    user = models.IntegerField()
    type = models.IntegerField(default=0)
    summary = models.CharField(max_length=500, null=True)
    content = models.TextField()
    content_show = models.TextField(u'正文显示', null=True)
    start_date = models.DateField()
    end_date = models.DateField()
    add_date = models.DateTimeField()
    status = models.IntegerField(default=0, choices=TYPE)
    schedule = models.IntegerField(default=0)  # 进度
    is_great = models.IntegerField(default=0)  # 是否精华
    counts = models.IntegerField(default=0)  # 点击率

    def __unicode__(self):
        return self.title

    class Meta:
        db_table = 'theme'

    def getUser(self):
        """返回用户对象"""
        return User.objects.get(pk=self.user)

    def getLove(self):
        """返回关注数"""
        return Follow.objects.filter(theme=self.id).count()

    def getLoveObj(self):
        """返回关注数"""
        return Follow.objects.filter(theme=self.id).values_list('user', flat=True)

    def getTags(self):
        """获取标签"""
        return ThemeTag.objects.filter(theme=self.id)

    def getType(self):
        """获取类型"""
        return Type.objects.get(pk=self.type, user=self.user)


class Tag(models.Model):
    """个人标签"""
    name = models.CharField(max_length=100)
    user = models.IntegerField()
    add_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'tags'


class ThemeTag(models.Model):
    """主题标签"""
    theme = models.ForeignKey(Theme)
    tag = models.ForeignKey(Tag)
    add_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.tag.name

    class Meta:
        db_table = 'theme_tag'


class Signal(models.Model):
    """公告"""
    type = models.IntegerField(default=0)  # 0,系统公告；1:评论;2:私信;3.关注;4.主题关注
    obj = models.IntegerField(default=0)  # 对象id
    user = models.ForeignKey(User)  # 发布者
    who = models.IntegerField(default=0)  # 接受者
    title = models.CharField(max_length=200, null=True)  # 标题
    content = models.CharField(max_length=1000, null=True)  # 内容
    status = models.IntegerField(default=0)  # 状态，0，未读；1已读，2.删除
    add_date = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'user_signal'

    def get_signal(self):
        """获取实例对象"""
        if self.type in (1, 4):
            return Theme.objects.get(pk=self.obj)
        return None


class Share(models.Model):
    title = models.CharField(max_length=255)
    url = models.URLField()
    created_datetime = models.DateTimeField(auto_now=True)
    user = models.ForeignKey(User)

    class Meta:
        db_table = 'share'

    def get_user(self):
        return User.objects.get(pk=self.user)
